//
//  LunbotuDetailViewController.m
//  BeautyClother
//
//  Created by dllo on 15/8/28.
//  Copyright (c) 2015年 宋爽. All rights reserved.
//

#import "LunbotuDetailViewController.h"
#import "AsyncConnection.h"
#import "define.h"
#import "UIImageView+WebCache.h"
#import "ImageSaveViewController.h"
#import "ImageWithLabel.h"
#import "womenDetailViewController.h"
#import "UMSocial.h"


@interface LunbotuDetailViewController ()<UITableViewDataSource, UITableViewDelegate, UMSocialUIDelegate>

@property (nonatomic, retain) UITableView *table;
@property (nonatomic, retain) NSMutableArray *array;
@property (nonatomic, retain) NSMutableDictionary *dic;
@property (nonatomic, assign) CGFloat offHeigt;
@property (nonatomic, assign) CGFloat offWidth;

@end

@implementation LunbotuDetailViewController

- (void)dealloc
{
    [_dic release];
    [_table release];
    [_array release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.array = [NSMutableArray array];
        self.dic = [NSMutableDictionary dictionary];
        self.offHeigt = [[UIScreen mainScreen]bounds].size.height / 667;
        self.offWidth = [[UIScreen mainScreen]bounds].size.width / 375;
    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self beginConnection];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.title = @"Item Detail";
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"f.png"] style:UIBarButtonItemStyleDone target:self action:@selector(back:)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor darkGrayColor];
    // Do any additional setup after loading the view.
}

- (void)back:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)beginConnection
{
    NSDictionary *dic = @{@"token":@"A2C8A7F0-5A1D-4614-9F42-9455C27499CB",
                          @"iid":self.iid,
                          @"item_type":@"item",
                          @"ua":@"iPhone%7C1.7.11",
                          @"lang":@"en",
                          @"sig":self.sig,
                          @"nt":@"wifi"};
    [AsyncConnection asyncConnection:LUNBOTU_DETAIL parmaters:dic block:^(NSData *data) {
        self.dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        [self createTableView];
    }path:@"main_lunbotu_detail_detail"];
}
//http://api.palshock.cn/trend/item/detail.psj
//http://api.palshock.cn/trend/item/detail.psj?token=A2C8A7F0-5A1D-4614-9F42-9455C27499CB&iid=239328&item_type=item&ua=iPhone%7C1.7.11&lang=en&sig=6677dfd28d081440751309812&nt=wifi
- (void)createTableView
{
    self.table = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    [self.view addSubview:self.table];
    self.table.delegate = self;
    self.table.dataSource = self;
    self.table.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.table release];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 0;
    }
    else
    {
        return 6;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"reuse"];
        if (indexPath.row == 0) {
            UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 80 * _offWidth, 80 * _offHeigt)];
            image.layer.cornerRadius = 80 *_offWidth / 2;
            image.layer.masksToBounds = YES;
            [cell.contentView addSubview:image];

            UILabel *title = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, self.view.frame.size.width - 80, 50 * _offHeigt)];
            [cell.contentView addSubview:title];
            title.text = [self.dic objectForKey:@"title"] ;
            title.font = [UIFont systemFontOfSize:16];
            [title release];
            UILabel *price = [[UILabel alloc]initWithFrame:CGRectMake(title.frame.size.width + 15, 0, 70, 50 *_offHeigt)];
            [cell.contentView addSubview:price];
            price.text = [@"$" stringByAppendingString:[[[[self.dic objectForKey:@"prices"]objectAtIndex:0]objectForKey:@"prc"]stringValue]];
            price.font = [UIFont systemFontOfSize:16];
            [price release];

            cell.textLabel.font = [UIFont systemFontOfSize:16];
        }
        if (indexPath.row == 1) {
            cell.textLabel.text = [[[[self.dic objectForKey:@"brand"]objectForKey:@"brE"] stringByAppendingString:@"         Site:  "] stringByAppendingString:[self.dic objectForKey:@"from"]];
            cell.backgroundColor = [UIColor whiteColor];
            cell.textLabel.font = [UIFont systemFontOfSize:15];
            cell.textLabel.textColor = [UIColor lightGrayColor];
            cell.alpha = 0.6;
        }
        if (indexPath.row == 2) {
            cell.textLabel.text = [self.dic objectForKey:@"desc"];
            cell.textLabel.textColor = [UIColor lightGrayColor];
            cell.textLabel.font = [UIFont systemFontOfSize:13];
        }
        
        NSArray *a = @[@"che.png", @"fen.png",@"BUY", @"SHARE"];
        if (indexPath.row == 3) {
            
            
            for (int i = 0; i < 2; i ++) {
                ImageWithLabel *image = [[ImageWithLabel alloc]initWithFrame:CGRectMake(130 *_offWidth * i + 70 * _offWidth, 0, 150 * _offWidth, 100 *_offHeigt)];
                [cell.contentView addSubview:image];
                
                image.ima.image = [UIImage imageNamed:[a objectAtIndex:i]];
                image.title.text = [a objectAtIndex:i + 2];
                if ([image.title.text isEqual:@"BUY"]) {
                    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(buy:)];
                    image.userInteractionEnabled = YES;
                    [image addGestureRecognizer:tap];

                
                }
                
                if ([image.title.text isEqual:@"SHARE"]) {
                    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(share:)];
                    image.userInteractionEnabled = YES;
                    [image addGestureRecognizer:tap];

                }
                [image release];
                
            }
        }
        if (indexPath.row == 4) {
            cell.textLabel.text = @"Similar Items";
        }
        if (indexPath.row == 5) {
            cell.textLabel.text = @"Relevant Sets";
        }
        cell.textLabel.numberOfLines = 0;
        cell.userInteractionEnabled = YES;
        
        
    }
    return cell;
}



- (void)buy:(UIButton *)sender
{
    
    womenDetailViewController *wareWeb = [[womenDetailViewController alloc]init];
    NSString *str = [self.dic objectForKey:@"bUrl"];
    NSURL *url = [NSURL URLWithString:str];
    wareWeb.url = url;
    [self.navigationController pushViewController:wareWeb animated:YES];
    [wareWeb release];
}

- (void)share:(UITapGestureRecognizer *)sender
{
    [UMSocialSnsService presentSnsIconSheetView:self
                                         appKey:@"55e57550e0f55a57db000e09"
                                      shareText:((ImageWithLabel *)sender.view).title.text
                                     shareImage:((ImageWithLabel *)sender.view).ima.image
                                shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToTencent,UMShareToRenren,nil]
                                       delegate:self];

    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        
    
    CGFloat width = [[[[self.dic objectForKey:@"images"]objectAtIndex:0]objectForKey:@"wd"]floatValue];
    CGFloat height = [[[[self.dic objectForKey:@"images"]objectAtIndex:0]objectForKey:@"ht"]floatValue];
    return self.view.frame.size.width * height / width + 64;
    }else
    {
        return 5;
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        return 50 * _offHeigt;
    }
    if (indexPath.row == 1) {
        return 40 * _offHeigt;
    }
    
    if (indexPath.row == 2 || indexPath.row == 3) {
        return 100 * _offHeigt;
    }if (indexPath.row == 4) {
        return 44;
    }else
    {
        return 44;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UITableViewHeaderFooterView *myHeader = [tableView dequeueReusableCellWithIdentifier:@"header"];
    if (myHeader == nil) {
        if (section == 0) {
            CGFloat width = [[[[self.dic objectForKey:@"images"]objectAtIndex:0]objectForKey:@"wd"]floatValue];
            CGFloat height = [[[[self.dic objectForKey:@"images"]objectAtIndex:0]objectForKey:@"ht"]floatValue];
            myHeader = [[UITableViewHeaderFooterView alloc]initWithReuseIdentifier:@"header"];
            UIImageView *ii = [[UIImageView alloc]initWithFrame:CGRectMake(0, 69, self.view.frame.size.width, self.view.frame.size.width * height / width)];
            ii.userInteractionEnabled = YES;
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(save:)];
            [ii addGestureRecognizer:tap];
            [tap release];
            
            [myHeader addSubview:ii];
            [ii sd_setImageWithURL:[NSURL URLWithString:[[[self.dic objectForKey:@"images"]objectAtIndex:0] objectForKey:@"url"]] placeholderImage:[UIImage imageNamed:@"zw.jpg"]];
        }
        
    }
    
    
    return myHeader;
}

- (void)save:(UITapGestureRecognizer *)save
{
    ImageSaveViewController *iiii = [[ImageSaveViewController alloc]init];
    iiii.image = ((UIImageView *)save.view).image;
    CGFloat width = [[[[self.dic objectForKey:@"images"]objectAtIndex:0]objectForKey:@"wd"]floatValue];
    CGFloat height = [[[[self.dic objectForKey:@"images"]objectAtIndex:0]objectForKey:@"ht"]floatValue];
    iiii.width = width;
    iiii.height = height;
    [self.navigationController pushViewController:iiii animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
